package com.tencent.qcloud.tuikit.tuichat.ui.GoodsList;


import android.content.Context;
import android.media.Image;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cqyc.network.model.GoodsInfo;
import com.tencent.qcloud.tuicore.util.ScreenUtil;
import com.tencent.qcloud.tuikit.tuichat.R;

import java.util.ArrayList;
import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodsHolder> {

    private Context context;

    public GoodsAdapter(Context context) {
        this.context = context;
    }

    private List<GoodsInfo> list = new ArrayList<>();

    public void setData(List<GoodsInfo> list) {
        this.list.clear();
        if (list != null && list.size() > 0) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addData(List<GoodsInfo> list) {
        if (list != null && list.size() > 0) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsAdapter.GoodsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
        return new GoodsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsAdapter.GoodsHolder holder, int position) {
        holder.setOnSendGoods(onSendGoods);
        holder.setOnItemGoods(onItemGoods);
        holder.setData(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class GoodsHolder extends RecyclerView.ViewHolder {

        private OnSendGoods onSendGoods;
        private OnItemGoods onItemGoods;

        public void setOnSendGoods(OnSendGoods onSendGoods) {
            this.onSendGoods = onSendGoods;
        }

        public void setOnItemGoods(OnItemGoods onItemGoods) {
            this.onItemGoods = onItemGoods;
        }

        private ImageView imageView;
        private TextView name;
        private TextView price;
        private View sendBtn;

        public GoodsHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);
            sendBtn = itemView.findViewById(R.id.send_btn);
        }

        public void setData(GoodsInfo data) {
            Glide.with(imageView).load(data.getItemImg()).into(imageView);
            name.setText(data.getItemName());
            String pr = data.getSalesPrice() + "yc";
            SpannableString spannableString = new SpannableString(pr);
            spannableString.setSpan(new AbsoluteSizeSpan(ScreenUtil.dip2px(17)), pr.length() - 2, pr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            price.setText(spannableString);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemGoods!=null){
                        onItemGoods.onItemGoods(data);
                    }
                }
            });
            sendBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onSendGoods!=null) {
                        onSendGoods.sendGoods(data);
                    }
                }
            });
        }
    }

    private OnSendGoods onSendGoods;
    private OnItemGoods onItemGoods;

    public void setOnSendGoods(OnSendGoods onSendGoods) {
        this.onSendGoods = onSendGoods;
    }

    public void setOnItemGoods(GoodsAdapter.OnItemGoods onItemGoods) {
        this.onItemGoods = onItemGoods;
    }

    public static interface OnSendGoods {
        void sendGoods(GoodsInfo data);
    }

    public interface OnItemGoods{
        void onItemGoods(GoodsInfo data);
    }

}
